package com.wudizaba.qt.quartz.job.Scheduler;

import com.wudizaba.qt.quartz.job.ScheduledTaskJob;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SchedulerOne implements ApplicationRunner {
    private static final String JOB_ID = "job";
    public static final String JOB_GROUP = "group";

    private static final String TRIGGER_ID = "job";
    public static final String TRIGGER_GROUP = "group";
    @Autowired
    private Scheduler scheduler;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 任务实例
        JobDetail jobDetail = JobBuilder.newJob(ScheduledTaskJob.class)
                .withIdentity(JOB_ID + " 01",JOB_GROUP+"01")//参数1：任务名，参数2：分组
                .build();

        //cron表达式 每隔一分钟执行一次
        CronScheduleBuilder scheduleBuilder =
                CronScheduleBuilder.cronSchedule("0 0/1 * * * ? ");


        // 任务触发器
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(TRIGGER_ID + " 01", TRIGGER_GROUP + "01")
                .withSchedule(scheduleBuilder)// 使用cron表达式,每隔一分钟执行一次
//                .startNow()// 立即执行
                .build();

        // 手动将触发器与任务绑定到调度器内
        scheduler.scheduleJob(jobDetail, trigger);

        // 启动调度器
        scheduler.start();
    }
}
